﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace KuPeiXun.Utility
{
    /// <summary>
    /// 加密工具
    /// </summary>
    /// ===============================================================
    /// 作者：邹学良 
    /// 时间：2013-03-21
    /// 描述：创建
    /// 历史：无
    /// ===============================================================
    public class EncryptionHepler
    {
        #region MD5加密
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="encryptText"></param>
        /// <returns></returns>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        public static string EncryptByMD5(string encryptText)
        {
            string result = string.Empty;
            if (encryptText != string.Empty)
            {
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] bytValue, bytHash;
                bytValue = System.Text.Encoding.UTF8.GetBytes(encryptText);
                bytHash = md5.ComputeHash(bytValue);
                md5.Clear();
                for (int i = 0; i < bytHash.Length; i++)
                {
                    result += bytHash[i].ToString("x");
                }
            }
            return result;
        }
        #endregion

        #region Base64加密
        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="encryptText"></param>
        /// <returns></returns>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        public static string EncryptByBase64(string encryptText)
        {
            string result = string.Empty;
            if (encryptText != string.Empty)
            {
                byte[] bytes = System.Text.Encoding.Default.GetBytes(encryptText);
                result = Convert.ToBase64String(bytes);
            }
            return result;
        }
        #endregion

        #region Base64解密
        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="decipherText"></param>
        /// <returns></returns>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        public static string DecipherByBase64(string decipherText)
        {
            string result = string.Empty;
            if (decipherText != string.Empty)
            {
                byte[] basenstr = Convert.FromBase64String(decipherText);//将64位转换为8位
                result = System.Text.Encoding.Default.GetString(basenstr);//解码为一个字符串            
            }
            return result;
        }
        #endregion
        
        #region Des加密
        /// <summary>
        /// 默认密钥向量
        /// </summary>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        private readonly static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>
        /// Des加密 密钥加密
        /// </summary>
        /// <param name="encryptText">加密的内容</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        public static string EncryptByDec(string encryptText, string key)
        {
            string result = string.Empty;
            if (key.Length < 8)
            {
                for (int i = key.Length; i <= 8; i++)
                {
                    key += "-";
                }
            }

            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

            byte[] keyIV = Keys;

            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptText);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);

            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            result = Convert.ToBase64String(mStream.ToArray());
            return result;
        }
        #endregion

        #region Des解密
        /// <summary>
        /// Des解密   密钥解密
        /// </summary>
        /// <param name="encryptText">揭秘内容</param>
        /// <param name="key">解密密钥</param>
        /// <returns></returns>
        /// ===============================================================
        /// 作者：邹学良 
        /// 时间：2013-03-21
        /// 描述：创建
        /// 历史：无
        /// ===============================================================
        public static string DecipherByDec(string encryptText, string key)
        {
            string result = string.Empty;

            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));

            byte[] keyIV = Keys;
            byte[] inputByteArray = Convert.FromBase64String(encryptText);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();

            result = Encoding.UTF8.GetString(mStream.ToArray());
            return result;
        }
        #endregion
    }
}
